<?php namespace App\Http\Controllers\common;
/**
 * Created by PhpStorm.
 * User: ender
 * Date: 15/6/2
 * Time: 下午4:04
 */
use App\Http\Controllers\Controller;


class LoraController extends Controller{
    public $username;
    public $password;
    public $time;
    public $key;
    public $url;

    //初始化属性
    public function __construct()
    {
        $this -> username = "hnqykj";
        $this -> password = '123456';
        $this -> time = time();
        $this -> key = 'turboes20160930';
        $this -> url = "http://lora.iqykj.com/Public/api_v2";
    }

    //获取验签
    public function getSign(){
        return md5($this->time.$this->username.md5($this->password).$this->key);
    }

    //添加网关
    public function addGateway($ng_nickname,$gw_id,$ng_model,$tx_power){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2001,
            'gw_id'=>$gw_id,
            'ng_nickname'=>$ng_nickname,
            'ng_model'=>$ng_model,
            'freq_band'=>'CN470',
            'tx_power'=>$tx_power,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //修改网关信息
    public function editGateway($gw_id,$ng_model,$tx_power){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2010,
            'gw_id'=>$gw_id,
            'ng_model'=>$ng_model,
            'freq_band'=>'CN470',
            'tx_power'=>$tx_power,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //获取网关信息
    public function getGatewayMsg($gw_id){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2002,
            'gw_id'=>$gw_id,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //重启网关
    public function restartGateway($gw_id){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2004,
            'gw_id'=>$gw_id,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //删除网关
    public function delGateway($gw_id){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2003,
            'gw_id'=>$gw_id,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //添加终端
    public function addTerminal($devEui,$appEui,$devAddr,$nwkSkey,$appSkey){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2005,
            'dev_eui'=>$devEui,
            'app_eui'=>$appEui,
            'dev_addr'=>$devAddr,
            'nwk_skey'=>$nwkSkey,
            'app_skey'=>$appSkey,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //批量添加终端信息
    public function addMostTerminal($list){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2011,
            'list'=>$list,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //获取终端信息
    public function getTerminalMsg($devEui){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2007,
            'dev_eui'=>$devEui,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //向终端发送应用数据
    public function sendToTerminal($devEui,$fprt,$data){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2009,
            'dev_eui'=>$devEui,
            'fprt'=>$fprt,
            'data'=>$data,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //删除终端
    public function delTerminal($devEui){
        $post_data = array (
            "username" => $this -> username,
            'protocol'=> 2008,
            'dev_eui'=>$devEui,
            'time'=>$this -> time,
            'sign'=>$this -> getSign()
        );
        return $this -> request($post_data);
    }

    //post请求lora.iqykj.com
    public function request($post_data){
        $data_string = json_encode($post_data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($data_string))
        );
        $output = curl_exec($ch);
        curl_close($ch);
        //打印获得的数据
        return $output;
    }
}